package com.kui.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.kui.entity.Qikan;
import com.kui.entity.Type;
import com.kui.entity.Wenxian;
import com.kui.service.QikanService;
import com.kui.service.TypeService;
import com.kui.service.WenxianService;

public class JingqueServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			
		
		WenxianService ws = new WenxianService();
		QikanService qs = new QikanService();
		TypeService ts = new TypeService();
		List<Wenxian> wenxianlist = ws.findAll();
		List<String> namelist = new ArrayList<String>();
		
		
		List<Qikan> qikanlist = qs.findAll();
		for(Qikan q : qikanlist){
			namelist.add('"'+q.getName()+'"');
			
		}
		
		List<Type> typelist = ts.findAll();
		for(Type t : typelist){
			namelist.add('"'+t.getName()+'"');
		}
		
		
		
		
		
		for(Wenxian w : wenxianlist){
			
			namelist.add('"'+w.getName()+'"');
			String[] names = w.getAuthor().split("，");
			for (int i = 0; i < names.length; i++) {
				String name = names[i];
				namelist.add('"'+name+'"');
			}
			namelist.add('"'+w.getId()+'"');
			String[] keywords = w.getKeyword().split("，");
			for (int i = 0; i < keywords.length; i++) {
				String keyword = keywords[i];
				namelist.add('"'+keyword+'"');
			}
			
		}
		
		//去除重复的检索词
		List<String> namelist1 = new ArrayList<String>();
		String name = "";
		for(String names : namelist){
			name = names;
			if(!(namelist1.contains(name))){
				namelist1.add(name);
			}
		}
		request.setAttribute("namelist", namelist1);
		
		request.getRequestDispatcher("WEB-INF/views/jingque.jsp").forward(request, response);
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String data = request.getParameter("data");
		String str = "";
		String mj = request.getParameter("mj");
		for (int i = 0; i < data.length(); i++) {
			str += data.substring(i,i+1) + "%" ;
		}
		
		String tag = request.getParameter("tag");
		System.out.println("str:"+str);
		
			WenxianService ws = new WenxianService();
			List<Wenxian> wenxianlist = new ArrayList<Wenxian>();
		if("name".equals(tag)){
			
			if("mohu".equals(mj)){
				wenxianlist = ws.findLikeName(str);
			}else{
				wenxianlist = ws.findLikeName(data);
			}
			
			
		}else if("author".equals(tag)){
			if("mohu".equals(mj)){
				wenxianlist = ws.findByLike(str);
			}else{
				wenxianlist = ws.findByLike(data);
			}
			
			
		}else if("qikan".equals(tag)){
			if("mohu".equals(mj)){
				wenxianlist = ws.findLikeQikan(str);
			}else{
				wenxianlist = ws.findLikeQikan(data);
			}
			
			
		}else if("type".equals(tag)){
			if("mohu".equals(mj)){
				wenxianlist = ws.findLikeType(str);
			}else{
				wenxianlist = ws.findLikeType(data);
			}
			
		}else if("id".equals(tag)){
			if("mohu".equals(mj)){
				wenxianlist = ws.findLikeId(str);
			}else{
				wenxianlist = ws.findLikeId(data);
			}
			
		}
			
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		String json = gson.toJson(wenxianlist);
		out.print(json);
		out.flush();
		out.close();
	}

}
